package com.fingard.dsp.bank.directbank.cmbc01;

import com.fingard.dsp.bank.directbank.cmbc01.CMBC01Base;
import com.fingard.dsp.bank.directbank.cmbc01.CMBC01Header;
import com.fingard.dsp.bank.directbank.bankInterface.IMailedMsg;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.reqrespobj.mailedmsg.ReqMailedMsgQry;
import com.fingard.dsp.bank.reqrespobj.mailedmsg.ReqMailedMsgQryDetail;
import com.fingard.dsp.bank.reqrespobj.mailedmsg.RetAtsMailedMsg;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;
@Component("CMBC01MailedMsg")
public class CMBC01MailedMsg extends CMBC01Base implements IMailedMsg{

	public static BASE64Decoder decoder;

	@Override
	public RetAtsMailedMsg getMailedMsg(ReqMailedMsgQry p_orderMsg) throws Exception {
		RetAtsMailedMsg retAts = new RetAtsMailedMsg(p_orderMsg.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
		String tmpRespInfo = "";
		try {
			int pageNum = 40;
			int sendNum = 0;
			int i = 0;
			if(StringHelper.isNullOrEmpty(p_orderMsg.Z_BISHU)) {
				retAts.writeRespCode("-1");
				retAts.writeRespInfo("总笔数未填写，不允许发送交易");
				retAts.writeEnd();
				return retAts;
			}
			int total = Integer.valueOf(p_orderMsg.Z_BISHU);
			//每次推送40条
			int pageSize = total/pageNum;
			while(i < pageSize + 1) {
				CMBC01Header bankHeader = new CMBC01Header();
				bankHeader.trnCode = "XR210009";
				XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
				// 写入通用的头部节点
				writeCommonHeader(xmlWriter, bankHeader);
				// 写入body
				xmlWriter.writeStartElement("xDataBody");
				xmlWriter.writeElementString("xssqdh", p_orderMsg.DetailId);// 申请单 ID
				xmlWriter.writeElementString("xskhdm",p_orderMsg.DealerCode);// 特约店代码
				xmlWriter.writeElementString("xsrzlx",getBankConfig().getValueAsString("financeType"));// 01-银票，02-贷款
				xmlWriter.writeElementString("xsphdm",p_orderMsg.BillNo);// 票据号/借据号

				while (sendNum < pageNum  && p_orderMsg.readRecord()) {
					sendNum ++;
					ReqMailedMsgQryDetail tmpDetail = p_orderMsg.ReqDetail;
					XElement tmpXElement = new XElement("detail", "");
					tmpXElement.addChild(new XElement("kcddbh", p_orderMsg.DetailId + sendNum));// 申请单ID
					tmpXElement.addChild(new XElement("xsvinm", tmpDetail.VIN));// VIN码
					tmpXElement.addChild(new XElement("kcfdjh", tmpDetail.EngineNo));// 发动机号
					tmpXElement.addChild(new XElement("kchgzh", tmpDetail.CertificationNo));// 合格证号
					tmpXElement.addChild(new XElement("kcgsmc", tmpDetail.Kcgsmc));//生成厂家
					tmpXElement.addChild(new XElement("kcppmc", tmpDetail.CarTypeCode));// 物品品牌
					tmpXElement.addChild(new XElement("kcxlmc", tmpDetail.motorcycleType));// 物品车型

					tmpXElement.addChild(new XElement("kcysdm", tmpDetail.ColorCode));// 颜色
					tmpXElement.addChild(new XElement("xsddhm", tmpDetail.OrderCode));// 订单号
					tmpXElement.addChild(new XElement("kccpdj", tmpDetail.PickupPrice));// 市场指导价
					tmpXElement.addChild(new XElement("kcddsl", "1"));// 订单数量
					tmpXElement.addChild(new XElement("xszxje", tmpDetail.PickupPrice));// 订单执行金额
					tmpXElement.addChild(new XElement("xsrzkk", tmpDetail.PickupPrice));// 订单执行金额
					tmpXElement.addChild(new XElement("kckksj", tmpDetail.deliveryTime));// 扣款时间
					tmpXElement.addChild(new XElement("kczkbl", "0"));// 标准折扣（标准折扣率）
					tmpXElement.addChild(new XElement("xtczbz", "01"));// 处 理 标 志 （ 01 ： 发运；02：送达；10：退车）
					tmpXElement.addChild(new XElement("xstjsj", tmpDetail.deliveryTime));// 发运时间
					xmlWriter.writeXElement(tmpXElement);
				}
				xmlWriter.writeEndElement();
				xmlWriter.writeEndDocument();
				String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());// 发送银行
				if (tmpStrRet[0].length() == 0) {
					StringBuilder tmpSbMsg = new StringBuilder();
					// 解析银行返回
					// base64解码
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
					if (tmpBkReader != null) {
						String ans_code = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
						String ans_info = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
						if ("0".equals(ans_code)) {
							String rsp_status = tmpBkReader.getFirstText("/CMBC/xDataBody/xtcsbz");
							if ("01".equals(rsp_status)) {
								while (tmpBkReader.read("/CMBC/xDataBody/detailList/Detail")) {
									String tmpReturnMsg = tmpBkReader.readedXElement.getSingleTextNotNull("xtsbyy");
									String orderNo = tmpBkReader.readedXElement.getSingleTextNotNull("xsddhm");
									tmpSbMsg.append(orderNo + ":" + tmpReturnMsg);
								}
								tmpRespCode = "0";
								tmpRespInfo = ans_info;
								sendNum = 0;
								i++;
							} else {
								tmpRespCode = "-1";
								tmpRespInfo = ans_info;
								break;
							}
						} else {
							tmpRespCode = "-1";
							tmpRespInfo = ans_info;
							break;
						}
					} else {
						tmpRespCode = "-1";
						tmpRespInfo = tmpSbMsg.toString();
						break;
					}
				} else {
					tmpRespCode = "-1";
					tmpRespInfo = tmpStrRet[1];
					break ;
				}
			}
		} catch (Exception e) {
			tmpRespCode = "-1";
			tmpRespInfo = e.getMessage();
			e.printStackTrace();
		}
		retAts.writeRespCode(tmpRespCode);
		retAts.writeRespInfo(tmpRespInfo);
		retAts.writeEnd();
		return retAts;
	}

}
